﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NibbleOil.Web.Account
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
        {
            if (CurrentPassword.Text.Equals(((Business.Model.USUARIO)Session["User"]).Clave))
            {
                Business.Model.USUARIO oUser = (Business.Model.USUARIO)Session["User"];
                oUser.Clave = NewPassword.Text;

                Business.Helper.User.Update(oUser);

                string sEmailFrom = string.Empty;
                if (!string.IsNullOrEmpty(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailFrom)))
                    sEmailFrom = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailFrom);

                string sSubject = string.Empty;
                if (!string.IsNullOrEmpty(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.ChangeyPassEmailSubject)))
                    sSubject = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.ChangeyPassEmailSubject);

                string sBody = string.Empty;
                if (!string.IsNullOrEmpty(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.ChangePassEmailBody)))
                {
                    sBody = string.Format(Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.ChangePassEmailBody), oUser.Clave);
                }

                bool bResult = Business.Helper.Mail.SendMail(sEmailFrom, oUser.Email, sSubject, sBody);
                SucessText.Text = string.Empty;

                if (bResult)
                {
                    CustomValidator2.ErrorMessage = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailMsgOk);
                    CustomValidator2.IsValid = false;
                }
                else
                {
                    CustomValidator1.ErrorMessage = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.EmailMsgError);
                    CustomValidator1.IsValid = false;
                }
            }
            else
            {
                CustomValidator1.ErrorMessage = Business.Helper.Parameter.GetValue(Business.Enumeration.ParameterEnum.Name.ChangePassKeyError);
                CustomValidator1.IsValid = false;
            }

        }

        protected void CancelPushButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/authentication/Default.aspx");
        }
    }
}
